package org.jash.comment

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView

class CommentAdapter<D>(val layout:(D) -> Pair<Int, Int>, val data: MutableList<D> = mutableListOf() ): RecyclerView.Adapter<CommentViewHolder>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): CommentViewHolder = CommentViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.context), viewType, parent, false))

    override fun getItemViewType(position: Int): Int {
        return layout(data[position]).first
    }
    override fun onBindViewHolder(
        holder: CommentViewHolder,
        position: Int
    ) {
        val d = data[position]
        val variableId = layout(d).second
        holder.binding.setVariable(variableId, d)
    }

    override fun getItemCount(): Int = data.size

    fun addData(vararg d: D) {
        val size = data.size
        data.addAll(d)
        notifyItemRangeInserted(size, d.size)
    }
}
class CommentViewHolder(val binding: ViewDataBinding): RecyclerView.ViewHolder(binding.root)